package models;
import java.text.Format;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.*;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import controllers.JsonDateFormatter;
import controllers.JsonDatetimeFormatter;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import controllers.Helper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
import models.Staff;
@Entity
@Table(name="part_purchase")
public class Part_purchase extends Model {
	private static final long serialVersionUID = 1L;
	@Id
	public Integer id; //编号
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date purchase_date; //采购日期
	@ManyToOne
	public Staff purchaser; //采购人
	@ManyToOne
	public Staff approver; //审批人
	
	public String invoice_number; //发票号/收据
	
	public String supplier; //供应商名称联系人及电话
	
	public String material; //材料名称、型号、品牌
	
	public Integer quantity; //数量
	
	public Double unit_price; //单价
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date send_date; //送货日期
	@ManyToOne
	public Staff accepter; //收货验收人
	
	public String comments; //采购说明
	public static Model.Finder<Integer, Part_purchase> find = new Model.Finder<Integer, Part_purchase>(Integer.class, Part_purchase.class);

	public static Page<Part_purchase> page(int page, int pageSize, String sortBy, String order, String [] filterColum, String[] filter) {
		ExpressionList<Part_purchase> exp = find.where();
		for(int i= 0; i< filterColum.length; i++) {
			String[] tokens = filter[i].split("\\|", 3);
			if (tokens.length == 0) {
				continue;
			}
			String fieldtype = tokens[0];
			if (tokens.length == 3) { //two values, range search
				if (tokens[1].isEmpty() || tokens[2].isEmpty() ) {
					continue;
				}
				Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
				Object obj1 = Helper.stringToObject(fieldtype, tokens[2]);
				exp = exp.between(filterColum[i], obj0, obj1);
			} else if (tokens.length == 2) { //one value search
				if (tokens[1].isEmpty()) {
					continue;
				}
				if (fieldtype.equalsIgnoreCase("String")) {
					exp = exp.ilike(filterColum[i], "%" + tokens[1] + "%");
				} else {
					Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
					exp = exp.eq(filterColum[i], obj0);
				}
			}
		}
		return exp.orderBy(sortBy + " " + order).findPagingList(pageSize).setFetchAhead(false).getPage(page);
	}
	public void setId(Integer d) {
		id= d;
	}
	public Integer getId() {
		return id;
	}
	public void setPurchase_date(Date d) {
		purchase_date= d;
	}
	public Date getPurchase_date() {
		return purchase_date;
	}
	public void setPurchaser(Staff d) {
		purchaser= d;
	}
	public Staff getPurchaser() {
		return purchaser;
	}
	public void setApprover(Staff d) {
		approver= d;
	}
	public Staff getApprover() {
		return approver;
	}
	public void setInvoice_number(String d) {
		invoice_number= d;
	}
	public String getInvoice_number() {
		return invoice_number;
	}
	public void setSupplier(String d) {
		supplier= d;
	}
	public String getSupplier() {
		return supplier;
	}
	public void setMaterial(String d) {
		material= d;
	}
	public String getMaterial() {
		return material;
	}
	public void setQuantity(Integer d) {
		quantity= d;
	}
	public Integer getQuantity() {
		return quantity;
	}
	public void setUnit_price(Double d) {
		unit_price= d;
	}
	public Double getUnit_price() {
		return unit_price;
	}
	public void setSend_date(Date d) {
		send_date= d;
	}
	public Date getSend_date() {
		return send_date;
	}
	public void setAccepter(Staff d) {
		accepter= d;
	}
	public Staff getAccepter() {
		return accepter;
	}
	public void setComments(String d) {
		comments= d;
	}
	public String getComments() {
		return comments;
	}
	public static Part_purchase findById(Integer d) {
		return find.byId(d);
	}
	public static String getIdJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Part_purchase v: find.all()) {
			node.put(v.getId().toString(), v.getId().toString()); 
		}
		return node.toString();
	}
}
